﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using OSA.Policlinic.BusinessApplication.Registry;
using Policlinica;
using NLog;

namespace OSA.Policlinic.UI.WFApplication
{
    public partial class ShowPatientView : UserControl
    {
        private  IRegistryBusinessApplication registryBusinessApplication;
        private List<Patient> patients;
        private mainForm owner;
        private static Logger logger = LogManager.GetCurrentClassLogger();

        public ShowPatientView(mainForm form)
        {
            InitializeComponent();
            registryBusinessApplication = RegistryBusinessApplicationHome.GetInstanse();
            patients = registryBusinessApplication.GetAllPatients();
            patientBindingSource.DataSource = patients;
            owner = form;
            logger.Trace("Контрол ShowPatientView успешно инициализирован");
        }

        private void PatientAddButton_Click(object sender, EventArgs e)
        {
            PatientForm patientForm = new PatientForm(owner);
            logger.Debug("Нажата кнопка 'Прикрепить нового пациента' контрола ShowPatientView");
            patientForm.ShowDialog();
            PatientsDataGridView.Rows.Clear();
            patients = registryBusinessApplication.GetAllPatients();
            patientBindingSource.DataSource = patients;
        }

        private void PatientSearchButton_Click(object sender, EventArgs e)
        {
            if (SeriesTextBox.Text.Trim().Length != 0 && NumberTextBox.Text.Trim().Length != 0)
            {
                List<Patient> p = registryBusinessApplication.GetPatientByPolisNumber(SeriesTextBox.Text,
                                                                                      NumberTextBox.Text);
                if (p != null && p.Count > 0)
                {
                    PatientForm patientForm = new PatientForm(owner, p[0]);
                    logger.Debug("Произведен поиск пациента по полису серия {0} номер {1}", SeriesTextBox.Text, NumberTextBox.Text);
                    patientForm.ShowDialog();
                    patientBindingSource.DataSource = patients;
                }
            }
        }

        private void SearchBySurnameButton_Click(object sender, EventArgs e)
        {
            if (SurnameTextBox.Text.Trim().Length != 0)
            {
                patients = registryBusinessApplication.GetPatientsBySurname(SurnameTextBox.Text);
                PatientsDataGridView.Rows.Clear();
                patientBindingSource.DataSource = patients;
                logger.Debug("Произведен поиск пациентов по фамилии {0}", SurnameTextBox.Text);
            }
        }

        private void ClearButton_Click(object sender, EventArgs e)
        {
            patients = registryBusinessApplication.GetAllPatients();
            patientBindingSource.DataSource = patients;
        }

        private void PatientsDataGridView_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            Patient p = (Patient)PatientsDataGridView.Rows[e.RowIndex].DataBoundItem;
            PatientForm patientForm = new PatientForm(owner, p);
            logger.Debug("Для просмотра данных выбран пациент id = {0}", p.Id);
            patientForm.ShowDialog();
            patientBindingSource.DataSource = patients;             
        }

        private void PatientsDataGridView_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Insert)
            {
                if (PatientsDataGridView.SelectedCells.Count > 0)
                {
                    Patient p = (Patient)PatientsDataGridView.CurrentRow.DataBoundItem;
                    PatientForm patientForm = new PatientForm(owner, p);
                    logger.Debug("Для просмотра данных выбран пациент id = {0}", p.Id);
                    patientForm.ShowDialog();
                    patientBindingSource.DataSource = patients;
                }
            }
        }
    }
}
